package com.huangpuweb.api.service.imp;

import com.huangpuweb.api.model.*;
import com.huangpuweb.api.service.PersonDataService;
import com.mysql.jdbc.TimeUtil;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Service
public class PersonDataServiceImpl extends BaseSysDataServiceImp implements PersonDataService {
    @Override
    public List<LhryInfo> getCoordinateByAddress() {
        String sql = "select zjhm,hjdz from laihu_count";
        return getEntryList(sql, new LhryInfoMapper(), new Object[]{});
    }

    @Override
    public  Map getSegmentByAge() {
        Map map=new HashMap(16);
        String sql="select cjrq,sz,mc from app_qqssmzrknltj   order by  cjrq desc limit 7";
        List<HpPersonHouseStatistics> entryList = getEntryList(sql, new RowMapper<HpPersonHouseStatistics>() {
            @Override
            public HpPersonHouseStatistics mapRow(ResultSet resultSet, int i) throws SQLException {
                HpPersonHouseStatistics hp = new HpPersonHouseStatistics();
                hp.setSz(resultSet.getInt("sz"));
                hp.setMc(resultSet.getString("mc"));
                hp.setCjrq(resultSet.getString("cjrq"));
                return hp;
            }
        }, new Object[]{});
        map.put("list",entryList);
        String sql1="select sum(a.sz) sum from (select cjrq,sz,mc from app_qqssmzrknltj   order by  cjrq desc limit 7 )a";
        Integer integer = queryInteger(sql1, new Object[]{});
        map.put("total",integer);
        return map;
    }

    @Override
    public List<HpBasSyrkJzdz> geLngLat() {
        String sql="select lng,lat from hp_syrk_isnothan_ethnic where id>0";
        return getEntryList(sql, new RowMapper<HpBasSyrkJzdz>() {
            @Override
            public HpBasSyrkJzdz mapRow(ResultSet resultSet, int i) throws SQLException {
                HpBasSyrkJzdz hpBasSyrkJzdz=new HpBasSyrkJzdz();
                hpBasSyrkJzdz.setLng(resultSet.getString("lng"));
                hpBasSyrkJzdz.setLat(resultSet.getString("lat"));
                return hpBasSyrkJzdz;
            }
        },new Object[]{});
    }

    @Override
    public List<AppDsjzxQqssrk> getMinorityByStreet() {
        String sql="select mc,jd,MAX(cjrq) cjrq,sz from app_dsjzx_jdssmz group by jd,mc order by jd desc\n";
        return getEntryList(sql, new RowMapper<AppDsjzxQqssrk>() {
            @Override
            public AppDsjzxQqssrk mapRow(ResultSet resultSet, int i) throws SQLException {
                AppDsjzxQqssrk appDsjzxQqssrk=new AppDsjzxQqssrk();
                appDsjzxQqssrk.setMc(resultSet.getString("mc"))
                        .setJd(resultSet.getString("jd"))
                        .setCjrq(resultSet.getString("cjrq"))
                        .setSz(resultSet.getInt("sz"));
                return appDsjzxQqssrk;
            }
        },new Object[]{});
    }

    @Override
    public int getHuJi(String type) {
        String sql="select sz from app_dsjzx_qqssrk where mc=?";
        return queryInteger(sql,new Object[]{type});
    }

    @Override
    public List<HpBasSyrkJzdz> getMinorityPersonPoint() {
        String sql="select lng,lat from hp_syrk_isnothan_ethnic where id>=1 ";
        return getEntryList(sql, new RowMapper<HpBasSyrkJzdz>() {
            @Override
            public HpBasSyrkJzdz mapRow(ResultSet resultSet, int i) throws SQLException {
                HpBasSyrkJzdz hpBasSyrkJzdz=new HpBasSyrkJzdz();
                hpBasSyrkJzdz.setLng(resultSet.getString("lng"));
                hpBasSyrkJzdz.setLat(resultSet.getString("lat"));
                return hpBasSyrkJzdz;
            }
        },new Object[]{});
    }

    /**
     * 获取实有人口数据
     * @return
     */
    public List<HpSyrkStreet> getHpSyrkStreetList(){
        String sql = "select * from hp_syrk_street where mc = \"实有人口\" and cjrq = (select cjrq from  hp_syrk_street where mc = \"实有人口\" ORDER BY cjrq desc limit 1);";
        return getEntryList(sql, new RowMapper<HpSyrkStreet>() {
            @Override
            public HpSyrkStreet mapRow(ResultSet resultSet, int i) throws SQLException {
                HpSyrkStreet hpSyrkStreet=new HpSyrkStreet();
                hpSyrkStreet.setId(resultSet.getInt("id"));
                hpSyrkStreet.setMc(resultSet.getString("mc"));
                hpSyrkStreet.setSz(resultSet.getInt("sz"));
                hpSyrkStreet.setLy(resultSet.getString("ly"));
                hpSyrkStreet.setJd(resultSet.getString("jd"));
                hpSyrkStreet.setCjrq(resultSet.getString("cjrq"));
                hpSyrkStreet.setDataOperatedate(resultSet.getString("data_operatedate"));
                hpSyrkStreet.setDataOperatetype(resultSet.getString("data_operatetype"));
                hpSyrkStreet.setServiceDt(resultSet.getString("service_dt"));
                hpSyrkStreet.setDataSrc(resultSet.getString("data_src"));
                hpSyrkStreet.setSysLoadDt(resultSet.getString("sys_load_dt"));
                hpSyrkStreet.setIsReload(resultSet.getString("is_reload"));
                hpSyrkStreet.setSysReloadDt(resultSet.getString("sys_reload_dt"));
                hpSyrkStreet.setDateId(resultSet.getString("date_id"));
                return hpSyrkStreet;
            }
        },new Object[]{});
    }

    @Override
    public List<HpBMedical> getHospitalShelter() {
        String sql="select yljgmc,jgdz,POINT_X,POINT_Y from b_medical ";
        return getEntryList(sql, new RowMapper<HpBMedical>() {
            @Override
            public HpBMedical mapRow(ResultSet resultSet, int i) throws SQLException {
                HpBMedical hp=new HpBMedical();
                hp.setPOINT_X(resultSet.getString("POINT_X"));
                hp.setPOINT_Y(resultSet.getString("POINT_Y"));
                hp.setYljgmc(resultSet.getString("yljgmc"));
                hp.setJgdz(resultSet.getString("jgdz"));
                return hp;
            }
        }, new Object[]{});
    }

    @Override
    public List<HpBasSyrkJzdz> getPersonPoint(String i) {
        switch (i){
            case "1": String sql="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 1 AND 30000 ";
                return getEntryList(sql,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "2": String sql2="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 30001 AND 60000 ";
                return getEntryList(sql2,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "3": String sql3="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 60001 AND 90000 ";
                return getEntryList(sql3,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "4": String sql4="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 120001 AND 150000 ";
                return getEntryList(sql4,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "5": String sql5="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 150001 AND 180000 ";
                return getEntryList(sql5,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "6": String sql6="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 180001 AND 210000 ";
                return getEntryList(sql6,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "7": String sql7="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 210001 AND 240000 ";
                return getEntryList(sql7,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "8": String sql8="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 270001 AND 300000 ";
                return getEntryList(sql8,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "9": String sql9="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 300001 AND 330000 ";
                return getEntryList(sql9,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "10": String sql10="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 330001 AND 370000 ";
                return getEntryList(sql10,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "11": String sql11="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 370001 AND 400000 ";
                return getEntryList(sql11,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            case "12": String sql12="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 400001 AND 430000 ";
                return getEntryList(sql12,new HpBasSyrkJzdzRowMapper(),new Object[]{});
            default:String sql13="SELECT lat,lng FROM hp_bas_syrk_jzdz WHERE id BETWEEN 430001 AND 482804 ";
                return getEntryList(sql13,new HpBasSyrkJzdzRowMapper(),new Object[]{});
        }

    }

    @Override
    public List<HpBasSyrkJzdz> getList() {
        String sql="select jzdz,id from hp_syrk_isnothan_ethnic";
        return getEntryList(sql, new RowMapper<HpBasSyrkJzdz>() {
            @Override
            public HpBasSyrkJzdz mapRow(ResultSet resultSet, int i) throws SQLException {
                HpBasSyrkJzdz sysHpPark=new HpBasSyrkJzdz();
                sysHpPark.setJzdz(resultSet.getString("jzdz"));
                sysHpPark.setId(resultSet.getInt("id"));
                return sysHpPark;
            }
        },new Object[]{});
//        String sql="SELECT id,jzdz FROM hp_bas_syrk_jzdz WHERE id between 318653 AND 482804";
//        return getEntryList(sql, new RowMapper<HpBasSyrkJzdz>() {
//            @Override
//            public HpBasSyrkJzdz mapRow(ResultSet resultSet, int i) throws SQLException {
//                HpBasSyrkJzdz hpBasSyrkJzdz = new HpBasSyrkJzdz();
//                hpBasSyrkJzdz.setId(resultSet.getInt("id"));
//                hpBasSyrkJzdz.setJzdz(resultSet.getString("jzdz"));
////                hpBasSyrkJzdz.setLat(resultSet.getString("lat"));
////                hpBasSyrkJzdz.setLng(resultSet.getString("lng"));
//                return hpBasSyrkJzdz;
//            }
//        },new Object[]{});
    }

    @Override
    public void insert(String lng, String lat,int code) {
//        String sql = "update  laihu_count set lng=?,lat=? where zjhm=? ";
        String sql="update  hp_syrk_isnothan_ethnic set lng=?,lat=? where id=?";
        update(sql, new Object[]{lng, lat,code});

    }
}
